package com.ggb.reactive.service

import com.ggb.reactive.domain.vo.DishItemVO
import lombok.RequiredArgsConstructor
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux

@Service
@RequiredArgsConstructor
class SetmeaDishService(
    private val databaseClient: DatabaseClient
) {
   /**
     * 根据套餐id获取套餐菜品
     */

   //fun getDishItemBySetmealId(setmealId: Long): Flux<DishItemVO> {
   //    val sql = "SELECT sd.name, sd.copies, d.image, d.description FROM setmeal_dish sd LEFT JOIN dish d ON sd.dish_id = d.id WHERE sd.setmeal_id = :setmealId"
   //    return databaseClient.sql(sql)
   //        .bind("setmealId", setmealId)
   //        .map { row -> DishItemVO(row.get("name", String::class.java)!!, row.get("image", String::class.java)!!, row.get("description", String::class.java)!!, row.get("copies", Int::class.java)!!) }
   //        .all()
   //}

   fun getDishItemBySetmealId(setmealId: Long): Flux<DishItemVO> {
       val sql = "SELECT sd.name, sd.copies, d.image, d.description FROM setmeal_dish sd LEFT JOIN dish d ON sd.dish_id = d.id WHERE sd.setmeal_id = $setmealId"
       return databaseClient.sql(sql)
           .map { row -> DishItemVO(row.get("name", String::class.java)!!, row.get("image", String::class.java)!!, row.get("description", String::class.java)!!, row.get("copies", Int::class.java)!!) }
           .all()
   }

}